var common = {};
common.isWord = function () {
    var cs = captureScreen();
    if (images.detectsColor(cs, "#ECFFFF", 50/540*device.width, 480/960*device.height, 16, "diff")) {
        cs.recycle();
        return true;
    } else {
        cs.recycle();
        return false;
    }
};

common.random = function (max, min) {
    return Math.random() * (max - min) + min;
};
common.getPoints = function (x, pr) {
    var img = captureScreen();
    var points = images.findAllPointsForColor(img, "#023C54", {
        region: [x, 220, 1, 460],
        threshold: 16
    })
    var pMin = 0;
    var pMax = 0;
    for (var i = 0; i < points.length; i++) {
        var p = points[i];
        if (pMin == 0) {
            pMin = p.y
        }
        if (pMax == 0) {
            pMax = p.y
        }
        if (p.y - pMax < 10) {
            pMax = p.y
        } else {
            if (pMax - pMin > 10) {
                pr.push({x: p.x, y: (pMax - pMin) / 2 + pMin})
            }
            pMin = p.y
            pMax = p.y
        }

        if (i == points.length - 1) {
            if (pMax - pMin > 10) {
                pr.push({x: p.x, y: (pMax - pMin) / 2 + pMin})
            }

        }
    }
};
common.getPoint = function (x) {
    var img = captureScreen();
    var point = images.findColor(img, "#00CBFF", {
        region: [x, 145, 1, 540],
        threshold: 16
    })
    return point;

};
common.getClipBtn = function () {
    var img = captureScreen();
    var point = images.findColor(img, "#157CAB", {
        region: [530, 220, 260, 25],
        threshold: 16
    })
    img.recycle();
    return point;
};
common.getCastle = function () {
    var img = captureScreen();
    var point = images.findColor(img, "#B5BAA9", {
        region: [480, 320, 300, 200],
        threshold: 16
    })
    return point;
};
common.waitColorVisible = function (x, y, color) {
    while (true) {
        if (images.detectsColor(captureScreen(), color, x, y, 16, "diff")) {
            break
        } else {
            sleep(100);
        }
    }
};
common.waitImgVisibleInRegion = function (tmplImgPath, x, y, w, h) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    while (!points) {
        // img = captureScreen();
        //console.log("未找到匹配模板:" + tmplImgPath)
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
    }
    // img.recycle();
    tmpl.recycle();
};
common.waitImgVisibleInRegionTimeOut = function (tmplImgPath, x, y, w, h) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    var i = 0;
    while (!points && i < 50) {
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
        i = i + 1
    }
    tmpl.recycle();
    return points
};
common.waitImgVisibleInRegionClick = function (tmplImgPath, x, y, w, h, xx, yy) {
    // var img = captureScreen();
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h]
    });
    var i = 0;
    while (!points && i < 50) {
        // img = captureScreen();
        //console.log("未找到匹配模板:" + tmplImgPath)
        points = findImage(captureScreen(), tmpl, {
            region: [x, y, w, h]
        });
        sleep(100);
        i = i + 1
    }
    if (i >= 50) {
        commandModule.press(xx, yy, 10)
        this.waitImgVisibleInRegion(tmplImgPath, x, y, w, h)
    }
    // img.recycle();
    tmpl.recycle();
};
common.imgExistInRegion = function (tmplImgPath, x, y, w, h) {
    var tmpl = images.read(tmplImgPath);
    var points = findImage(captureScreen(), tmpl, {
        region: [x, y, w, h],
        threshold: 0.95
    });
    tmpl.recycle();
    return points;
};

module.exports = common;